package com.hobee.basic.service.impl;

import com.hobee.basic.repository.ClassTypeRepository;
import com.hobee.basic.mapper.BdClassTypeMapper;
import com.hobee.basic.model.es.ClassType;
import com.hobee.basic.service.ClassTypeService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author junjie.li
 * @date 2020/7/29 17:55
 */
@Service
public class ClassTypeServiceImpl implements ClassTypeService {

    private BdClassTypeMapper bdclasstypeMapper;
    private ClassTypeRepository classTypeRepository;

    @Autowired
    public ClassTypeServiceImpl(BdClassTypeMapper bdclasstypeMapper, ClassTypeRepository classTypeRepository) {
        this.bdclasstypeMapper = bdclasstypeMapper;
        this.classTypeRepository = classTypeRepository;
    }

    @Override
    public void initData() {
        List<ClassType> classTypes = bdclasstypeMapper.queryAll();
        classTypeRepository.saveAll(classTypes);
    }

    @Override
    public List<ClassType> queryClassTypeByEs(String classtypeName, String projectId) {
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("classtypeName", classtypeName).minimumShouldMatch("75%");
        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("projectId", projectId);
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(termQueryBuilder).filter(matchQueryBuilder);
        Page<ClassType> search = classTypeRepository.search(boolQueryBuilder, PageRequest.of(0, 10));
        return search.getContent();
    }
}
